App({
  onLaunch(options) {
    // 第一次打开
    // options.query == {number:1}
    console.log(options)
    this.firstLogin()
  },
  onShow(options) {
    // 从后台被 scheme 重新打开
    // options.query == {number:1}
  },
  globalData:{
    baseurl: "http://test.api.dakecn.com",
    AppKey: "dingh0ofazej5ofynge2",
    AppSecret: "xx5By6_3hwIidZeqrI9xdDoDE16Vuf8MTCRRMeeJd1PE-ydqr6wpEEsYNWh9ozdl",
    Authorization: "",
    user: {}
  },
  req(options){
    var extant = ""
    var contextType = options.contextType || null
    var data = options.data || {}
    if(!options||!options.url){
      dd.alert("请输入正确的参数")
    }
    if(options.type){
      var method = options.type.toUpperCase()
    }else{
      var method = "GET"
    }
    if(method == "POST"){
      contextType = "application/json"
      data = JSON.stringify(data)
    }
    dd.httpRequest({
      url: this.globalData.baseurl + options.url,
      method: method,
      data: data,
      headers :{
        "Content-Type": contextType,
        "Authorization": this.globalData.Authorization
      },
      dataType: options.dataType || 'json',
      success: function(res) {
        options.cb(res)
        // dd.alert({content: 'success'});
      },
      fail: function(res) {
        if(res.status == 401){
          dd.alert({content: '登录超时，请重新登陆'});
          dd.showToast({
            type: 'fail',
            content: '登录超时',
            success(){
              dd.redirectTo({
                url: "/pages/login/login"
              })
            }
          });
        }
        if(res.status != 401 && typeof options.fail == "function"){
          options.fail(res)
        }
        console.log(res);
        
        // dd.alert({content: 'fail'});
      },
      complete: function(res) {
        if(typeof options.complete == "function"){
          options.complete(res)
        }
        // dd.alert({content: 'complete'});
      }
    });
  },
  // beParams(obj){
  //   var str = "?"
  //   for (const i in obj) {
  //     str += i + "=" + obj[i] + "&"
  //   }
  //   str = str.substr(0,str.length-1)
  //   console.log(str);
  //   return str;
  // },
  loginSuccess(options,res){
    var _this = this
    dd.setStorage({
      key: 'LoginNo',
      data: {
        LoginNo: options.LoginNo,
        Password: options.Password
      },
      success: function() {
        _this.globalData.Authorization = res.token
        _this.globalData.user = res.user
      }
    });
  },
  firstLogin(){
    var _this = this
    dd.getStorage({
      key: 'LoginNo',
      success: function(res) {
        if(res.data){
          _this.req({
            url: "/auth",
            type: "POST",
            data: {
              LoginNo: res.data.LoginNo,
              Password: res.data.Password
            },
            cb(res1){
              console.log(res1);
              if(res1.data.token){
                _this.loginSuccess({LoginNo: res.data.LoginNo,Password: res.data.Password},res1.data)
                _this.checkLoginEnd(res1.data.user)
              }
            },
            fail(res){
              dd.redirectTo({
                url: "/pages/login/login"
              })
            }
          })
        }else{
          dd.redirectTo({
            url: "/pages/login/login"
          })
        }
      },
      fail: function(res){
        dd.redirectTo({
          url: "/pages/login/login"
        })
      }
    })
  },
  // checkLoginEnd(res){

  // }
});
